/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	fetp.c
**  Description:		fpga middle transmit protocol	FPGA与中位机电机协议
**  Author:        	 
**  Version				v0.1
**  Date:           	2019-11-26
**	Function List： 
**  History:         
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/
#include "can_protocol.h"
/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	结构体定义
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	函数原型声明
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	全局变量定义
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	模块静态变量定义
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                              	函数定义
*********************************************************************************************************
*/
void can_read_reg(uint16_t can_id,uint8_t reg_addr,can_msg_t* p_msg)
{
	p_msg->can_id = (CAN_ID_MSK & can_id) | FN_SDO_T;
	p_msg->can_dlc = 2;
	p_msg->can_data[0] = SDO_CS_READ;
	p_msg->can_data[1] = reg_addr;
	return;
}
void can_write_reg(uint16_t can_id,uint8_t reg_addr,can_msg_t* p_msg)
{
	p_msg->can_id = (CAN_ID_MSK & can_id) | FN_SDO_T;
	p_msg->can_dlc = 6;
	p_msg->can_data[0] = SDO_CS_WRITE;
	p_msg->can_data[1] = reg_addr;
	return;
}
void can_nmt_sw(uint16_t can_id,nmt_status_t status,can_msg_t* p_msg)
{
	p_msg->can_id = FN_NMT_TIM|can_id;
	p_msg->can_dlc = 2;
	switch(status){
		case NMT_STATUS_ONLINE:p_msg->can_data[0] = NMT_SW_ONLINE;break;
		case NMT_STATUS_STOP:p_msg->can_data[0] = NMT_SW_STOP;break;
		case NMT_STATUS_WORK:p_msg->can_data[0] = NMT_SW_WORK;break;
		default:break;
	}
	
	p_msg->can_data[1] = can_id;
	return;
}
void can_sync(can_msg_t* p_msg)
{
	p_msg->can_id = FN_SYNC;
	p_msg->can_dlc = 1;
	p_msg->can_data[0] = 0;
	return;
}
uint8_t is_sdo_cmd(can_msg_t* p_msg)
{
	if((p_msg->can_id & CAN_ID_FN_MSK) == FN_SDO_T || (p_msg->can_id & CAN_ID_FN_MSK) == FN_SDO_R)
		return 1;
	return 0;
}
void can_read_reg_ack(uint16_t can_id,uint8_t reg_addr,can_msg_t* p_msg)
{
	p_msg->can_id = (CAN_ID_MSK & can_id) | FN_SDO_R;
	p_msg->can_dlc = 6;
	p_msg->can_data[0] = SDO_CS_READ_ACK;
	p_msg->can_data[1] = reg_addr;
	return;
}
void can_write_reg_ack(uint16_t can_id,uint8_t reg_addr,can_msg_t* p_msg)
{
	p_msg->can_id = (CAN_ID_MSK & can_id) | FN_SDO_R;
	p_msg->can_dlc = 6;
	p_msg->can_data[0] = SDO_CS_WRITE_ACK;
	p_msg->can_data[1] = reg_addr;
	return;
}
void can_err_ack(uint16_t can_id,uint8_t err_code,can_msg_t* p_msg)
{
	p_msg->can_id = (CAN_ID_MSK & can_id) | FN_SDO_R;
	p_msg->can_dlc = 2;
	p_msg->can_data[0] = SDO_CS_ERR;
	p_msg->can_data[1] = err_code;
	return;
}
